<template>
    <view>
		<!-- 默认学期设置组件 -->
        <button v-if="pickerType==='default'" class="cu-btn bg-orange-1 shadow" @click="modalName = 'model'">
            {{choiceSession}}
            <text class="cuIcon-right"></text>
        </button>
        <view class="cu-modal" :class="modalName=='model'?'show':''">
            <view class="cu-dialog">
                <view class="bg-white margin-sm">
                    <view class="text-left text-bold text-xl padding-sm">
                        学期选择
                    </view>
                    <view class="grid col-8 bg-white">
                        <view v-for="(item, index) in yearArray" :key="index" class="padding-xs">
                            <button class="cu-btn blue block" :class="item.title==xnm?'bg-blue':'line-blue'" @click="yearClick(index)">
                                {{item.title}}
                            </button>
                        </view>
                    </view>
                </view>
                <view class="cu-bar bg-white">
                    <view class="action margin-0 flex-sub text-black solid-left" @click="cancel">取消</view>
                    <view class="action margin-0 flex-sub text-green solid-left" @click="confirm">确定</view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
	import {
		$reqJson
	}from '../../utils/util.js'
    export default {
        name: "term-picker",
        data() {
            return {
                modalName: "",
                yearArray: [],
                termArray: [{
                    title: "全部学期",
                    value: 1,
                }],
                xnm: '2020-2021年第二学期',
				choiceSession:'2020-2021年第二学期',
				sessionId:1
            };
        },
        props: {
            pickerType: {
                type: String,
                default: "default"
            }
        },
        created() {
            this.yearArray = this.getYearArray()
            this.$emit("choiceSession", this.sessionId);
        },
        methods: {
            getYearArray: function() {
                let yearArray = []
					
                this.$reqJson('wxapi/session','get',{},res=>{
					for(let i =0;i < res.content.length;i++){
						let data={
							title:res.content[i].session,
							value:res.content[i].id
						}
						yearArray.push(data);
					}
				})             
				 yearArray.push({
					 title: "全部学年",
					 value: "",
				 })
                return yearArray.reverse()
            },
            yearClick(index) {
                this.choiceSession = this.yearArray[index].title.toString()
				this.sessionId=this.yearArray[index].value
				this.$emit("choiceSession", this.sessionId);
            },
            cancel() {
                this.modalName = null
                this.$emit("cancel");
            },
            confirm() {
                this.modalName = null
                this.$emit("confirm");
            }
        }
    }
</script>

<style lang="scss" scoped>
</style>
